// view/index/jk/shenqjiank/shenqjiank.js
let app = getApp();
let yuming = app.globalData.yuming;
let mobile = app.globalData.mobile;
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '申请设备',
    list: ['1台', '2台', '3台', '4台', '5台', '6台'],
    curr: 0,
    jine: 101,
    je: '101',
    fuwu: [],
    fucrrr: 0,
    mid: '', //服务商id
    fwf: false,
    check: false,
    write: false,
    userName: '', //姓名
    mobile: '', // 手机号
    shopAddress: '', // 地址
    aaa: '',
    gouxuan: false,
    ispaybox: false,
    bxprice: ''
  },
  yz() {
    if (this.data.userName == '') {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none'
      })
    } else if (this.data.userName.length < 2) {
      wx.showToast({
        title: '联系人不能少于两个字',
        icon: 'none'
      })
    } else if (this.data.mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (!mobile.test(this.data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    } else if (this.data.shopAddress == '') {
      wx.showToast({
        title: '请输入地址',
        icon: 'none'
      })
    } else {
      this.tijiao()
    }
  },
  // 提交
  tijiao() {
    let that = this
    this.myRequest(yuming + 'Monitor/shopAddMonitor', {
      shopId: wx.getStorageSync('shopId'),
      mid: that.data.mid,
      alPrice: that.data.jine,
      buyNum: that.data.curr + 1,
      shopAddress: that.data.shopAddress,
      userName: that.data.userName,
      mobile: that.data.mobile
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.setData({
          xkorderId: res.data.data.orderSn,
          ispaybox: true,
          write: false
        })
        setTimeout(() => {
          this.setData({
            ispaybox: false
          })
          this.pay();
        }, 3000)
      }
    })
  },
  // 模拟支付
  pay() {
    let that = this
    this.myRequest(yuming + 'Notify/pay_notify', {
      out_trade_no: that.data.xkorderId
    }, 'POST').then(res => {
      wx.hideLoading()
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        // console.log(res);
        // that.setData({
        //   write: false
        // })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 1000)
      }
    })
  },
  // 姓名
  userName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  // 手机号
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 地址
  shopAddress(e) {
    this.setData({
      shopAddress: e.detail.value
    })
  },
  write() {
    this.setData({
      write: !this.data.write
    })
  },
  // 勾选
  gouxuan() {
    var that = this
    that.setData({
      gouxuan: !that.data.gouxuan
    })
    if (that.data.gouxuan) {
      setTimeout(function () {
        that.setData({
          fwf: false,
          write: true
        })
      }, 1000)
    }
  },
  // 关闭服务费
  guanfwf() {
    this.setData({
      fwf: false
    })
  },
  // 列表
  gyslist() {
    var that = this
    this.myRequest(yuming + 'Monitor/shopSeeMonitor', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      that.setData({
        fuwu: res.data.data,
        mid: res.data.data[0].id,
        jine: res.data.data[0].price,
        je: res.data.data[0].price,
      })
    })
  },
  xuanfu(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      fucrrr: index,
      mid: this.data.fuwu[index].id,
      jine: this.data.fuwu[index].price * this.data.num,
      je: this.data.fuwu[index].price,
    })
  },
  // 支付
  zhifu(e) {
    // wx.showToast({
    //   title: '暂未开通',
    //   icon:'none'
    // })
    // console.log(e)
    var aaa = e.currentTarget.dataset.aaa;
    if (aaa == 0) {
      // 查看
      this.setData({
        fwf: !this.data.fwf
      })
    } else {
      // 支付
      if (this.data.gouxuan) {
        if (this.data.userName == '' || this.data.mobile == '' || this.data.shopAddress == '') {
          wx.showToast({
            title: '请填写相关信息',
            icon: 'none'
          })
          this.setData({
            write: true
          })
        }
      } else {
        this.setData({
          fwf: true
        })
      }
    }
  },
  xuan(e) {
    var index = e.currentTarget.dataset.index
    var num = (e.currentTarget.dataset.index) + 1
    this.setData({
      curr: index,
      num,
      jine: this.data.je * num
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  // 服务费说明
  xq() {
    var that = this
    this.myRequest(yuming + 'Besall/thatHomeAgreement', {
      place: 'sqjkfwfsm'
    }, 'POST').then(res => {
      that.setData({
        xq: res.data.data,
        content: res.data.data.content.replace(/(\<img|\<p)/gi, function ($0, $1) {
          return {
            "<img": '<img style="width:100%;height:auto;display:block;" ',
            "<p": '<p style="text-indent: 2em;" ',
            "<article": "<div",
            "</article": "</div",
            "<header": "<div",
            "</header": "</div"
          } [$1];
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
    this.gyslist()
    this.xq()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})